package com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.util;

import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.entity.BuildingVulnerability;
import com.css.fxfzfxqh.modules.util.repository.YSXRepository;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class Tools {


    @Autowired
    YSXRepository ysxRepository;

    /**
     * 计算面积
     *
     * @param v1 设防烈度占比
     * @param v2 总面积
     * @param v3 易损性值
     * @return
     */

    public static BigDecimal getArea(Double v1, Double v2, Double v3) {
        //计算当前设防状态的面积
        //获取当前设防状态占比
        if (PlatformObjectUtils.isEmpty(v3) || 0.00 == v3) {
            return BigDecimal.ZERO;
        }
        BigDecimal bigDecimal = BigDecimal.valueOf(v1).divide(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_UP);
        //根据当前占比计算面积
        BigDecimal bigDecimal1 = bigDecimal.multiply(BigDecimal.valueOf(v2)).setScale(2, BigDecimal.ROUND_UP);
        //根据易损性值计算最终结果
        BigDecimal bigDecimal3 = BigDecimal.valueOf(v3).divide(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_UP);
        BigDecimal bigDecimal2 = bigDecimal1.multiply(bigDecimal3).setScale(2, BigDecimal.ROUND_UP);
        return bigDecimal2;
    }

    //根据省市县获取本省市县下的所有易损性
    public Map<String, Map<String, List<BuildingVulnerability>>> getYSX(String pName, String cName, String name) {
        List<YSXEntity> list = ysxRepository.getYSX(pName, cName, name);
        if (list == null || list.size() < 1) {
            throw new RuntimeException("评估失败,请维护" + pName + "-" + cName + "-" + name + "的易损性数据");
        }
        Table<String, String, List<BuildingVulnerability>> ysxTable =  HashBasedTable.create();
        // BiMap sfIntensityMap = BiMap
        //collect的key是建筑物结构类型   value是相同结构类型的集合
        Map<String, List<YSXEntity>> collect = list.stream().collect(Collectors.groupingBy(YSXEntity::getStructure_type));
        collect.forEach((k,v)->{
            Map<String, List<YSXEntity>> fortifyMap = v.stream().collect(Collectors.groupingBy(YSXEntity::getFortify));
            //k1是设防烈度
            fortifyMap.forEach((k1,v1)->{
                List<BuildingVulnerability> vulnerabilityList = getBuildingVulnerabilityList(v1,k1);
                ysxTable.put(k,k1,vulnerabilityList);
            });
        });

        return ysxTable.rowMap();
    }

    private List<BuildingVulnerability> getBuildingVulnerabilityList(List<YSXEntity> v1, String k1) {
        List<BuildingVulnerability> vulnerabilityList = new ArrayList<>();
        BuildingVulnerability buildingVulnerability = new BuildingVulnerability();
        Double baisc = v1.get(0).getIntensity6();
        Double slight = v1.get(1).getIntensity6();
        Double mid = v1.get(2).getIntensity6();
        Double serious = v1.get(3).getIntensity6();
        Double destory = v1.get(4).getIntensity6();
        buildingVulnerability.setBasic(baisc == null ? BigDecimal.ZERO : BigDecimal.valueOf(baisc));
        buildingVulnerability.setSlight(slight == null ? BigDecimal.ZERO : BigDecimal.valueOf(slight));
        buildingVulnerability.setMid(mid == null ? BigDecimal.ZERO : BigDecimal.valueOf(mid));
        buildingVulnerability.setSerious(serious == null ? BigDecimal.ZERO : BigDecimal.valueOf(serious));
        buildingVulnerability.setDestroy(destory == null ? BigDecimal.ZERO : BigDecimal.valueOf(destory));
        buildingVulnerability.setSfIntensity(k1);
        buildingVulnerability.setIntensity("6");
        vulnerabilityList.add(buildingVulnerability);
        buildingVulnerability = new BuildingVulnerability();
        baisc = v1.get(0).getIntensity7();
        slight = v1.get(1).getIntensity7();
        mid = v1.get(2).getIntensity7();
        serious = v1.get(3).getIntensity7();
        destory = v1.get(4).getIntensity7();
        buildingVulnerability.setBasic(baisc == null ? BigDecimal.ZERO : BigDecimal.valueOf(baisc));
        buildingVulnerability.setSlight(slight == null ? BigDecimal.ZERO : BigDecimal.valueOf(slight));
        buildingVulnerability.setMid(mid == null ? BigDecimal.ZERO : BigDecimal.valueOf(mid));
        buildingVulnerability.setSerious(serious == null ? BigDecimal.ZERO : BigDecimal.valueOf(serious));
        buildingVulnerability.setDestroy(destory == null ? BigDecimal.ZERO : BigDecimal.valueOf(destory));
        buildingVulnerability.setSfIntensity(k1);
        buildingVulnerability.setIntensity("7");
        vulnerabilityList.add(buildingVulnerability);
        buildingVulnerability = new BuildingVulnerability();
        baisc = v1.get(0).getIntensity8();
        slight = v1.get(1).getIntensity8();
        mid = v1.get(2).getIntensity8();
        serious = v1.get(3).getIntensity8();
        destory = v1.get(4).getIntensity8();
        buildingVulnerability.setBasic(baisc == null ? BigDecimal.ZERO : BigDecimal.valueOf(baisc));
        buildingVulnerability.setSlight(slight == null ? BigDecimal.ZERO : BigDecimal.valueOf(slight));
        buildingVulnerability.setMid(mid == null ? BigDecimal.ZERO : BigDecimal.valueOf(mid));
        buildingVulnerability.setSerious(serious == null ? BigDecimal.ZERO : BigDecimal.valueOf(serious));
        buildingVulnerability.setDestroy(destory == null ? BigDecimal.ZERO : BigDecimal.valueOf(destory));
        buildingVulnerability.setSfIntensity(k1);
        buildingVulnerability.setIntensity("8");
        vulnerabilityList.add(buildingVulnerability);
        buildingVulnerability = new BuildingVulnerability();
        baisc = v1.get(0).getIntensity9();
        slight = v1.get(1).getIntensity9();
        mid = v1.get(2).getIntensity9();
        serious = v1.get(3).getIntensity9();
        destory = v1.get(4).getIntensity9();
        buildingVulnerability.setBasic(baisc == null ? BigDecimal.ZERO : BigDecimal.valueOf(baisc));
        buildingVulnerability.setSlight(slight == null ? BigDecimal.ZERO : BigDecimal.valueOf(slight));
        buildingVulnerability.setMid(mid == null ? BigDecimal.ZERO : BigDecimal.valueOf(mid));
        buildingVulnerability.setSerious(serious == null ? BigDecimal.ZERO : BigDecimal.valueOf(serious));
        buildingVulnerability.setDestroy(destory == null ? BigDecimal.ZERO : BigDecimal.valueOf(destory));
        buildingVulnerability.setSfIntensity(k1);
        buildingVulnerability.setIntensity("9");
        vulnerabilityList.add(buildingVulnerability);
        buildingVulnerability = new BuildingVulnerability();
        baisc = v1.get(0).getIntensity10();
        slight = v1.get(1).getIntensity10();
        mid = v1.get(2).getIntensity10();
        serious = v1.get(3).getIntensity10();
        destory = v1.get(4).getIntensity10();
        buildingVulnerability.setBasic(baisc == null ? BigDecimal.ZERO : BigDecimal.valueOf(baisc));
        buildingVulnerability.setSlight(slight == null ? BigDecimal.ZERO : BigDecimal.valueOf(slight));
        buildingVulnerability.setMid(mid == null ? BigDecimal.ZERO : BigDecimal.valueOf(mid));
        buildingVulnerability.setSerious(serious == null ? BigDecimal.ZERO : BigDecimal.valueOf(serious));
        buildingVulnerability.setDestroy(destory == null ? BigDecimal.ZERO : BigDecimal.valueOf(destory));
        buildingVulnerability.setSfIntensity(k1);
        buildingVulnerability.setIntensity("10");
        vulnerabilityList.add(buildingVulnerability);
        return vulnerabilityList;
    }

    //获取破坏状态map
    private void getDamageDegreeMap(YSXEntity ysxEntity, Map<String, YSXEntity> map) {
        //破坏状态
        switch (ysxEntity.getDamage_degree()) {
            case "基本完好":
                map.put("基本完好", ysxEntity);
                break;
            case "轻微破坏":
                map.put("轻微破坏", ysxEntity);
                break;
            case "中等破坏":
                map.put("中等破坏", ysxEntity);
                break;
            case "严重破坏":
                map.put("严重破坏", ysxEntity);
                break;
            case "倒塌":
                map.put("倒塌", ysxEntity);
                break;
        }
    }
}
